package com.njlime.core.job;

import com.alibaba.fastjson.JSONArray;
import com.njlime.sys.security.util.CacheUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * api初始化任务
 * Created by Dean on 2018-3-19.
 */
public class ApiJob {

    public void initApi(){
        BufferedReader reader = null;
        String lastStr = "";
        try{
            InputStream in = ApiJob.class.getResourceAsStream("/api.json");
            InputStreamReader inputStreamReader = new InputStreamReader(in, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String tempString = null;
            while((tempString = reader.readLine()) != null){
                lastStr += tempString;
            }
            reader.close();
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        JSONArray jsonArray = JSONArray.parseArray(lastStr);
        Map<String, Object> apiMap = new HashMap<>();
        for(int i = 0; i < jsonArray.size(); i++){
            apiMap.put(jsonArray.getJSONObject(i).getString("api"),jsonArray.getJSONObject(i));
        }
        CacheUtils.put("api", apiMap);
        System.out.println("初始化api信息完成");
    }
}
